﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Xml;
using System.ComponentModel;

namespace Ares.Engine
{
    public enum BackupMethod
    {
        full,
        differential,
        incremental,
        differentialbyte
    }

    [Serializable]
    public class BackupProject : INotifyPropertyChanged
    {
        private String name;

        //We either have a Folder or a single file
        public String Name {
            get
            {
                return name;
            }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
        public String Source { get; set; }
        public String Destination { get; set; }
        public String StorageEngine { get; set; }
        public Guid ProjectGuid { get; set; }
        public String Comment { get; set; }

        //Method
        public BackupMethod Method { get; set; }

        /// <summary>
        /// Event raised to notify that a property has changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event
        /// </summary>
        /// <param name="propertyName">The property name</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public BackupProject()
        {
            Method = BackupMethod.full;
        }

        public static BackupProject LoadBackupProject(string ProjectFile)
        {
            BackupProject tmpBP = new BackupProject();
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            TextReader tr = new StreamReader(ProjectFile);
            tmpBP = (BackupProject)serializer.Deserialize(tr);
            tr.Close();

            return tmpBP;
        }

        public static void SaveBackupProject(string ProjectFile, BackupProject BP)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            TextWriter tw = new StreamWriter(ProjectFile);
            serializer.Serialize(tw, BP);
            tw.Close();
        }

        public static BackupProject DeSerializeBackupProject(string XMLData)
        {
            BackupProject tmpBP = new BackupProject();
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            StringReader tr = new StringReader(XMLData);
            tmpBP = (BackupProject)serializer.Deserialize(tr);
            tr.Close();

            return tmpBP;
        }

        public static string SerializeBackupProject(BackupProject BP)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            StringWriter sw = new StringWriter();
            serializer.Serialize(sw, BP);
            string result = sw.ToString();
            return result;
        }
    }
}
